package cn.wolfcode.resultpage;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.ArrayList;
import java.util.List;
@Getter
@Setter
@ToString
public class ResultPage<T> {
    private int currentPage = 1; // 当前页
    private int pageSize = 3;  // 每页显示多少条数据

    private int totalCount; // 总条数
    private List<T> dataList; // 当前页的显示数据,使用<T>泛型返回的类型可以清楚看到查询的是哪个对象数据

    private int totalPage; // 总页数
    private int prevPage;
    private int nextPage;

    public ResultPage(int currentPage, int pageSize, int totalCount, List<T> dataList) {
        this.currentPage = currentPage;
        this.pageSize = pageSize;
        this.totalCount = totalCount;
        this.dataList = dataList;

        if(this.totalCount == this.pageSize ){
            this.prevPage = 1;
            this.nextPage = 1;
            this.totalPage = 1;
        }

        this.prevPage = currentPage - 1 >= 1 ? currentPage - 1 : 1;
        this.totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
        this.nextPage = currentPage + 1 <= totalPage ? currentPage + 1 : totalPage;
    }

    public static ResultPage emtry(int pageSize){
        return new ResultPage(1,pageSize,0,new ArrayList());
    }

}
